package com.hsb.practiceb;

public class Solution {
	public static void main(String[] args) {
		convert("IIV");
	}

	public static void convert(String roman) {
		boolean error = false;
		char[] array = roman.toCharArray();
		int total = 0;
		for (int i = 0; i < array.length; i++) {
			if (i != 0) {
				if (lower(array[i], array[i - 1])// V I
						|| equals(array[i], array[i - 1])) {// I I
					if (chartoint(array[i - 1]) != -1) {
						total += chartoint(array[i - 1]);// 1
					} else {
						error = true;
					}
				} else {
					if (i - 2 > -1) {
						if (lower(array[i - 2], array[i - 1])
								|| equals(array[i - 2], array[i - 1])) {
							error = true;
						} else {
							if (chartoint(array[i - 1]) != -1) {
								total -= chartoint(array[i - 1]);
							} else {
								error = true;
							}
						}
					}else{
						if (chartoint(array[i - 1]) != -1) {
							total -= chartoint(array[i - 1]);
						} else {
							error = true;
						}
					}
				}
			}
			if (i == array.length - 1) {
				if (chartoint(array[i]) != -1) {
					total += chartoint(array[i]);
				} else {
					error = true;
				}
			}
		}
		if (error) {
			String excetion = "\u5f02\u5e38";
			System.out.println(excetion);
		} else {
			System.out.println(total);
		}
	}

	private static boolean equals(char c, char d) {
		if (c == d) {
			return true;
		}
		return false;
	}

	public static int chartoint(char c) {
		if (c == 'I') {
			return 1;
		} else if (c == 'V') {
			return 5;
		} else if (c == 'X') {
			return 10;
		} else if (c == 'L') {
			return 50;
		} else if (c == 'C') {
			return 100;
		} else if (c == 'D') {
			return 500;
		} else if (c == 'M') {
			return 1000;
		} else {
			return -1;
		}
	}

	public static boolean lower(char a, char b) {
		if ((a == 'I' && b != 'I') || (a == 'V' && b != 'I' && b != 'V')
				|| (a == 'X' && b != 'I' && b != 'V' && b != 'X')
				|| (a == 'L' && b != 'I' && b != 'V' && b != 'X' && b != 'L')
				|| (a == 'C' && (b == 'D' || b == 'M'))
				|| (a == 'D' && b == 'M')) {
			return true;
		} else {
			return false;
		}
	}
}
